package org.kgc.alanhealth.service.impl;

import org.kgc.alanhealth.entity.HospitalUser;
import org.kgc.alanhealth.entity.HospitalUseraddress;
import org.kgc.alanhealth.mapper.HospitalUserMapper;
import org.kgc.alanhealth.mapper.HospitalUseraddressMapper;
import org.kgc.alanhealth.mapper.SessiontokenMapper;
import org.kgc.alanhealth.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private HospitalUserMapper hospitalUserMapper;
    @Autowired
    private SessionTokenServiceImpl service;
    @Autowired
    private HospitalUseraddressMapper useraddressMapper;

    @Override
    public HospitalUser getUserByUsername(String username, String password) {
        HospitalUser user = hospitalUserMapper.getUserByUsername(username);
        if (user != null && user.getUserpassword().equals(password)) {
            String token = service.insertSessionToken(username);
            user.setSessiontoken(token);
            return user;
        }
        return null;
    }

    @Override
    public HospitalUser getUserByUsername2(String username) {
        return hospitalUserMapper.getUserByUsername(username);
    }

    @Override
    public int insertSelective(HospitalUser record) {
        hospitalUserMapper.insertSelective(record);
        return 1;
    }

    @Override
    public List<HospitalUseraddress> getAddressByUserid(int id) {
        return useraddressMapper.getAddressByUserid(id);
    }

    @Override
    public HospitalUser getUserById(int id) {
        return hospitalUserMapper.selectByPrimaryKey(id);
    }
}